<?php
/**
 * Author:  songsong
 * Date:     2019/3/25 0025
 * Describe: 订单需要的方法总结
 */
namespace app\common\controller\order;
use think\Controller;
use think\Request;
use think\Log;
use think\Db;
class Orderget extends Controller
{
	
//获取订单实际还款金额
public function get_shiji_money($ApplyID){
	$where['Type'] = array('in', [1,2,4,5,6,7]);
	$where['ApplyID'] = $ApplyID;
	
	$shiji_money=Db::table('xb_loans_hklist')->where($where)->sum('TotalMoney');
	return  $shiji_money;
}
//	获取 $RoleID
public function get_RoleID(){
	
	$RoleID=Db::table('xb_sys_administrator')->where(['ID'=>session('id')])->value('RoleID');
	return  $RoleID;
}
//	不是公司总部 限制 显示数据
public function get_data($data){
	
	
}
	//后去角色列表角色列表
	public function get_role(){
		$data=Db::table('xb_sys_role')->select();
		$RoleID=Db::table('xb_sys_administrator')->where(['ID'=>session('id')])->value('RoleID');
		if($RoleID!='1'){
			foreach ($data as $k => $v) {
				if($v['ID']=='1'){
					unset($data[$k]);
				}
			}
		}
		return  $data;
	}
	/**
	 * 定时任务 日志写入
	 */
	public function logs($Des){
		$controller =request()->controller(); 
		$action = request()->action();
		$data = array(
			'DateTime'=>date('Y-m-d H:i:s'),
			'IP'=>'定时任务',
			'IPCity'=>'定时任务',
			'Des'=>$Des,
			'UserID'=>'',
			'TrueName'=>'定时任务',
			'UserName'=>'定时任务',
			"ControllerName"=>$controller,
			'ActionName'=>$action,
			'URL'=>'admin/'.$controller.'/'.$action
		);
		$result = Db::table('xb_sys_log')->insert($data);
	}
	/**
	 * 订单分配客服
	 * $RoleID 角色ID
	 */
	function get_kefu($RoleID){
      $where = [
            'RoleID' => $RoleID,               //角色ID
            'isjiqi' =>2,        //是否机器 1 是 2否
        ];
		
	$DShadminID=db('sys_administrator')->where($where)->column('ID'); //客服专员ID
	$count=count($DShadminID);
	if($count=='1'){
		return $DShadminID['0'];
	}else{
	 return $DShadminID[rand(0,$count-1)];
	}
}
	
	
	/**
	 * 借款次数
	 */
	 	public function get_Jktime($re){
	 	foreach ($re as $k => $v) {
	 	  $ret = Db::table('xb_loans_applylist')->where('UserID',$v['UserID'])->select();
			$Jktime= count($ret);
			$re[$k]['Jktime'] =$Jktime;
		}
		return $re;
	 	}
	/**
	 * 获取该订单的实际还款时间和预约还款时间
	 */
	 	public function get_SjFkTime_YyFkTime($re){
	 	foreach ($re as $k => $v) {
	 	   $where_oans_xjapplylist['OrderSn']=$v['OrderSn'];
			if($v['is_fujie']=='1'){//如果订单展期字段为1 就去查询展期表
				$PayTime=Db::table('xb_loans_xjapplylist')->where($where_oans_xjapplylist)->field('PayTime')->select();
			    $YyFkTime=Db::table('xb_loans_xjapplylist')->where($where_oans_xjapplylist)->field('YyFkTime')->select();
				$PayTimeaa='';
				$YyFkTimeaa=$v['YyFkTime2'];
				foreach ($PayTime as $kk => $vv) {
					$PayTimeaa=$PayTimeaa.$vv['PayTime'].'<span style="color:red">/</span> ';
				}
				foreach ($YyFkTime as $kk => $vv) {
					$YyFkTimeaa=$YyFkTimeaa.'<span style="color:red">/</span>'.$vv['YyFkTime'];
				}	
			   $re[$k]['SjFkTime']=$PayTimeaa.$v['SjFkTime'];
			   $re[$k]['YyFkTime']=$YyFkTimeaa;
			}
		}
		return $re;
	 	}
	/**
	 * 最新一条备注
	 */
	 	public function get_beizhu($re){
	 	foreach ($re as $k => $v) {
	 	   $beizhu=Db::table('xb_beizhu')->where(['AppID'=>$v['ID']])->order('ID desc')->find();
            if($beizhu){
            	$re[$k]['beizhu']=$beizhu['beizhu'];
            }else{
            	$re[$k]['beizhu']='';
            }
		}
		return $re;
	 	}
	/**
	 * 是否部分还款
	 */
	 	public function get_bufenhuankuan($re){
	 	foreach ($re as $k => $v) {
	 		if($v['bufenhuankuan']=='1'){
				$re[$k]['bufenhuankuan']='<span style="color:green">是</span>';
			}
			if($v['bufenhuankuan']=='2'){
				$re[$k]['bufenhuankuan']='<span style="color:gray">否</span> ';
			}
		}
		return $re;
	 	}
	/**
	 * 是否展期
	 */
	 	public function get_xjnums($re){
	 	foreach ($re as $k => $v) {
	 		if($v['xjnums']>=1){
				$re[$k]['xjnums_status']='<span style="color:green">展期</span>';
			}else{
				$re[$k]['xjnums_status']='<span style="color:blue">未展期</span>';
			}
		}
		return $re;
	 	}
	/**
	 * 客户类型
	 */
	 	public function get_user_type($re){
	 	foreach ($re as $k => $v) {
	 		if($v['user_type']==1){
				$re[$k]['user_type']='<span style="color:green">新客户</span>';
			}
			if($v['user_type']==2){
				$re[$k]['user_type']='<span style="color:blue">老客户</span>';
			}
		}
		return $re;
	 	}
	/**
	 * 审核客服
	 */
	 	public function get_ShadminID($re){
	 	foreach ($re as $k => $v) {
	 		 if($v['ShadminID']){
				$re[$k]['ShadminID']=Db::table('xb_sys_administrator')->where(['ID'=>$v['ShadminID']])->value('UserName');
			}
		}
		return $re;
	 	}
	/**
	 * 催收人
	 */
	 	public function get_FkadminID($re){
	 	foreach ($re as $k => $v) {
	 		 if($v['FkadminID']){
				$re[$k]['FkadminID']=Db::table('xb_sys_administrator')->where(['ID'=>$v['FkadminID']])->value('UserName');
			}
		}
		return $re;
	 	}
	/**
	 * 催收人
	 */
	 	public function get_cuishourenID($re){
	 	foreach ($re as $k => $v) {
	 		if($v['cuishourenID']){
				$re[$k]['cuishourenID']=Db::table('xb_sys_administrator')->where(['ID'=>$v['cuishourenID']])->value('UserName');
			}
		}
		return $re;
	 	}
	/**
	 * 订单类型
	 */
	 	public function get_app_type($re){
	 	foreach ($re as $k => $v) {
	 		if($v['app_type']=='1'){
				$re[$k]['app_type']='首借';
			}
			if($v['app_type']=='2'){
				$re[$k]['app_type']='复借';
			}
			if($v['app_type']=='3'){
				$re[$k]['app_type']='正常展期';
			}
			if($v['app_type']=='4'){
				$re[$k]['app_type']='逾期展期';
			}
		}
		return $re;
	
	 	}
	
	/**
	 * //支付方式（放款方式） 1微信 2支付宝 3银行卡(后台手动放款)4银行卡(自动放款)
	 */
	 	public function get_pay_pay($re){
	 	foreach ($re as $k => $v) {
	 		if($v['pay']=='1'){
				$re[$k]['pay']='<span style="color:green">微信</span>';
			}
			if($v['pay']=='2'){
				$re[$k]['pay']='<span style="color:green">支付宝</span>';
			}
			if($v['pay']=='3'){
				$re[$k]['pay']='<span style="color:green">银联手动</span>';
			}
			if($v['pay']=='4'){
				$re[$k]['pay']='<span style="color:green">银联自动</span>';
			}
		}
		return $re;
	
	 	}
	/**
	 * 获取 订单状态
	 */
	public function get_LoanStatus($re){
//		p($re);die;
	 	foreach ($re as $k => $v) {
	 		 if($v['LoanStatus']=='0'){
				$re[$k]['LoanStatus']='<span style="color:green">申请中</span>';
			}
		    if($v['LoanStatus']=='1'){
				$re[$k]['LoanStatus']='<span style="color:green">等待放款</span>';
			}
			if($v['LoanStatus']=='2'){
				$re[$k]['LoanStatus']='<span style="color:green">已放款</span>';
			}
			if($v['LoanStatus']=='3'){
				$re[$k]['LoanStatus']='<span style="color:black">已完成</span> ';
			}
			if($v['LoanStatus']=='4'){
				$re[$k]['LoanStatus']='<span>已取消</span>';
			}
			if($v['LoanStatus']=='5'){
				$re[$k]['LoanStatus']='<span style="color:red">已拒绝</span>';
			}
			if($v['LoanStatus']=='6'){
				$re[$k]['LoanStatus']='等待银行打款';
			}
			if($v['LoanStatus']=='7'){
				$re[$k]['LoanStatus']='<span style="color:blue">待人工审核</span>';
			}
		}
		return $re;
	}
	
	
	/**
	 * 获取 还款类型  和支付方式
	 */
	 public function get_Type_pay($re){
	 	foreach ($re as $k => $v) {
//			还款类型 1部分还款2结清3豁免 4线上还款 5线上展期 6线下还款7线下展期 
			if($v['Type']=='1'){
				$re[$k]['Type']='<span style="color:gray">部分还款</span> ';
			}
			if($v['Type']=='2'){
				$re[$k]['Type']='<span style="color:#bdbd42">结清</span>';
			}
			if($v['Type']=='3'){
				$re[$k]['Type']='<span style="color:#0000FF">豁免 </span>';
			}
			if($v['Type']=='4'){
				$re[$k]['Type']='<span style="color:yellor">线上还款</span>';
			}
			if($v['Type']=='5'){
				$re[$k]['Type']='<span style="color:blue">线上展期 </span>';
			}
			if($v['Type']=='6'){
				$re[$k]['Type']='<span style="color:green">线下还款</span>';
			}
			if($v['Type']=='7'){
				$re[$k]['Type']='<span style="color:#0000FF">线下展期 </span>';
			}
//			1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
			if($v['pay']=='1'){
				$re[$k]['pay']='<span style="color:#28004D">线下微信</span>';
			}
			if($v['pay']=='2'){
				$re[$k]['pay']='<span style="color:#0000FF">线下支付宝</span>';
			}
			if($v['pay']=='3'){
				$re[$k]['pay']='<span style="color:#00FF00">线下银联</span>';
			}
			if($v['pay']=='4'){
				$re[$k]['pay']='<span style="color:#00FFFF">线上微信</span>';
			}
			if($v['pay']=='5'){
				$re[$k]['pay']='<span style="color:#FF0000">线上支付宝</span>';
			}
			if($v['pay']=='6'){
				$re[$k]['pay']='<span style="color:yellor">线上银联</span>';
			}
			if($v['pay']=='7'){
				$re[$k]['pay']='<span style="color:#0000FF ">豁免</span>';
			}
		}
           return $re;
	 }
	/**
	 * 搜索功能  订单里面的搜索
	 * $sorch  搜索参数
	 * $sorch_aaa   搜索值
	 * $TgadminID 推广ID
	 */
	 public function sorch(){
		$data=input();//接收数据
	 	$where=[];
	 	if($data){
	 		if(!empty($data['sorch'])){
				if($data['sorch']=='2'){
					$where['TrueName'] = $data['sorch_aaa'];//姓名
				}
				if($data['sorch']=='3'){
					$where['Mobile'] =  $data['sorch_aaa'];//手机号码
				}
				if($data['sorch']=='4'){
					$where['IDCard'] =  $data['sorch_aaa'];//身份证号码
				}
	            if($data['sorch']=='5'){
					$where['a.OrderSn'] =  $data['sorch_aaa'];//订单号码
				}
				if($data['sorch']=='6'){
					$where['BankNo'] =  $data['sorch_aaa'];//银行卡号
				}
				if($data['sorch']=='7'){
					$where['IP'] =  $data['sorch_aaa'];//日志列表里面的IP查找
				}
	 		}
		}
		if(!empty($data['TgadminID'])){
			$where['TgadminID'] = $data['TgadminID'];//推广ID
		}
		if(!empty($data['Type'])){//还款类型
			$where['Type'] = $data['Type'];
		}
		if(!empty($data['pay'])){//还款方式
			$where['a.pay'] = $data['pay'];
		}
		if(!empty($data['user_type'])){//客户类型
			$where['user_type'] = $data['user_type'];
		}
		if(!empty($data['ApplyTime'])){//订单生成时间
			$ApplyTime=explode(' - ', $data['ApplyTime']);
	        $where['ApplyTime'] = ['between',[$ApplyTime['0'],$ApplyTime['1']]];
		}
		if(!empty($data['YyFkTime'])){//还款时间
			$YyFkTime=explode(' - ', $data['YyFkTime']);
	        $where['YyFkTime'] = ['between',[$YyFkTime['0'],$YyFkTime['1']]];
		}

        if(!empty($data['RegTime'])){//注册时间   注册未申请使用
			$RegTime=explode(' - ', $data['RegTime']);
	        $where['RegTime'] = ['between',[$RegTime['0'],$RegTime['1']]];
		}
        if(!empty($data['SjFkTime'])){//实际还款时间  还款未借使用
			$SjFkTime=explode(' - ', $data['SjFkTime']);
	        $where['SjFkTime'] = ['between',[$SjFkTime['0'],$SjFkTime['1']]];
		}
       if(!empty($data['DateTime'])){//日志操作时间
			$SjFkTime=explode(' - ', $data['DateTime']);
	        $where['DateTime'] = ['between',[$SjFkTime['0'],$SjFkTime['1']]];
		}
	    if(!empty($data['time'])){//充值时间
			$SjFkTime=explode(' - ', $data['time']);
	        $where['time'] = ['between',[$SjFkTime['0'],$SjFkTime['1']]];
		}
       if(!empty($data['loanStatus'])){//订单状态
	        $where['loanStatus']=$data['loanStatus'];
		}

		return $where;
	 }
	/**
 * 根据商品ID 获取详情
 * $ProductID  商品ID
 * 
 */
	public function get_goods($ProductID){
		$goods=db('goods')->where(['ID'=>$ProductID])->find();//商户号
		return $goods;
	}
    /**
     * 获取支付环境 1正式 2测试
     */
    public function get_Payceshi(){
	    $Payceshi=db('sys_basicinfo')->value('Payceshi');
		return $Payceshi;
    }
	 /**
     * 获取系统配置所有数据
     */
    public function get_basicinfo(){
	    $basicinfo=db('sys_basicinfo')->find();
		return $basicinfo;
    }
	
	 /**
     * 获取系统配置数据
     */
    public function get_cuishouren(){
	    $cuishouren=db('sys_basicinfo')->value('cuishouren');
		return $cuishouren;
    }
 /**
     * 获取系统配置数据
     */
    public function get_daikou(){
	    $daikou=db('sys_basicinfo')->value('daikou');
		return $daikou;
    }
      //获取商户号 1正式 2测试
    public function get_changpay(){
	    $changpay=db('sys_inteparameter')->where(['ParaName'=>'changpay'])->value('ParaValue');//商户号
	    return $changpay;
    }
	   //获取魔蝎token
    public function get_token_moxie(){
	    $token_moxie=db('sys_inteparameter')->where(['ParaName'=>'tokenmoxie'])->value('ParaValue');//商户号
	    return $token_moxie;
    }
	/**
	 * 渠道名称获取
	 */
	 	public function get_qudao($TgadminID){
	 	$TgadminID=Db::table('xb_tg_admin')->where(['ID'=>$TgadminID])->value('Name');
		return $TgadminID;
	 	}
	
	/**
	 * $time 初始时间 日期格式
	 * $days 增加天数  
	 * $YyFkTime 返回结果 日期格式 
	 */
	public function get_time($time,$days){
			$YyFkTime= strtotime($time); //预约还款日期转为时间戳
			$days=86400*$days;
			$YyFkTime=$YyFkTime+$days;
//			$YyFkTime=date('Y-m-d H:i:s',$YyFkTime);
			$YyFkTime=date("Y-m-d",$YyFkTime);
//			p($YyFkTime);die;
			return $YyFkTime;
	}
	/**
	 * $time 初始时间 日期格式
	 * $days 减少天数  
	 * $YyFkTime 返回结果 日期格式 
	 */
	public function get_time_jian($time,$days){
			$YyFkTime= strtotime($time); //预约还款日期转为时间戳
			$days=86400*$days;
			$YyFkTime=$YyFkTime-$days;
//			$YyFkTime=date('Y-m-d H:i:s',$YyFkTime);
			$YyFkTime=date("Y-m-d",$YyFkTime);
//			p($YyFkTime);die;
			return $YyFkTime;
	}
	
	
	/**
	 * $timelingche 当天凌晨的时间戳       时间戳格式   strtotime(date("Y-m-d"),time());//当天凌晨的时间戳
	 * $YyFkTime      预约还款日期          时间戳格式    strtotime($v['YyFkTime']); //预约还款日期转为时间戳
	 * $v   数组中的$v
	 * $v['yuqililv']   逾期率
	 * $v['ApplyMoney']  逾期金额
	 * $data f返回参数
	 * fakuan 罚款金额
	 * datas  逾期天数
	 */
	public function get_days_fakuan($timelingche,$YyFkTime,$v){
		$yuqijifeidays = db('sys_basicinfo')->value('yuqijifeidays');
            $datas=$timelingche-$YyFkTime;
			if($datas>0){
			 $datas=$datas/86400;
			 $datadays=$datas;//实际预期天数
				if($datas>$yuqijifeidays){
					$datas=$yuqijifeidays;//预期天数超过系统天数   就设为系统预定天数
				}
			}else{
				$datadays='0';
				$datas='0';
			}
            $data['fakuan']=$v['yuqililv']*$v['daoqiMoney']*$datas/100;
			$data['fakuan']= $data['fakuan'].'.00';
			$data['datas']= $datadays;
			return $data;
	}
	
	/**
	 * 获取该订单应还减免
	 * $OrderSn  订单号码
	 */
	 
	 public function get_jianmian_money($OrderSn){
	 	$loans_hklist=db('loans_hklist')->where(['OrderSn'=>$OrderSn,'Type'=>'3'])->select();//还款记录数据
		$jianmian_money='0';
		if($loans_hklist){
				foreach ($loans_hklist as $k => $v) {
					$jianmian_money=$v['TotalMoney']+$jianmian_money;
				}
			}
		return $jianmian_money;
	 }

	/**
	 * 获取该订单部分已还
	 * $OrderSn  订单号码
	 */
	 
	 public function get_bufen_money($OrderSn){
	 	$loans_hklist=db('loans_hklist')->where(['OrderSn'=>$OrderSn,'Type'=>'1'])->select();//还款记录数据
		$bufen_money='0';
		if($loans_hklist){
				foreach ($loans_hklist as $k => $v) {
					$bufen_money=$v['TotalMoney']+$bufen_money;
				}
			}
		return $bufen_money;
	 }
	/**
	 *获取该顶单当前应还本金
	 * $data  订单所有数据
	 */
	public function get_yinghuan_benjin($data){
		$loans_hklist=db('loans_hklist')->where('OrderSn',$data['OrderSn'])->select();//还款记录数据
			if($loans_hklist){
				$data['moneysheng']='0';
				foreach ($loans_hklist as $k => $v) {
					$data['moneysheng']=$v['TotalMoney']+$data['moneysheng'];
				}
					$data['moneysheng']=$data['ApplyMoney']-$data['moneysheng'];
					$data['moneysheng']=$data['moneysheng'].'.00';
			}else{
				$data['moneysheng']=$data['ApplyMoney'];//剩余应还金额
			}
			return $data['moneysheng'];
	}
	/**
	 * 获取该顶单当前应还逾期费
	 * $data  订单所有数据
	 */
	public function get_yinghuan_yuqi($data){
		$YyFkTime= strtotime($data['YyFkTime']); //预约还款日期转为时间戳
		$dangtianTime=time();//当日时间戳
		if($dangtianTime>$YyFkTime){  //开始计算逾期费
			$timess=$dangtianTime-$YyFkTime;
			$datys=$timess/86400;
			$datys=intval(floor($datys));
	        $yuqijifeidays=db('sys_basicinfo')->value('yuqijifeidays');//		获取系统配置里面的预期计费天数
	        if($datys>$yuqijifeidays){
	        	$datys='5';
	        }
	        $yuqililv=$data['yuqililv']/100;
			$data['yuqimoney']=$data['daoqiMoney']*$datys*$yuqililv;//逾期金额=逾期天数*逾期利率*借款金额
			$data['yuqimoney']=$data['yuqimoney'].'.00';
		}else{
			$data['yuqimoney']='0.00';
		}
		return $data['yuqimoney'];
	}
	/**
	 * 目前应还金额   本金+逾期费
	 * $data  订单所有数据
	 */
	public function get_yinghuan_money($data){
		$benjin=$this->get_yinghuan_benjin($data);
		$yuqi=$this->get_yinghuan_yuqi($data);
        $moneysheng=$yuqi+$benjin;
		return $moneysheng;
	}
	
	/**
	* 订单应还   到账金额+服务费+逾期费       到期金额+逾期费
	 * $data  订单全部数据
	*/
	public function get_dingdan_yinghuan_money($data){
		$yuqi=$this->get_yinghuan_yuqi($data);
		$dingdan_yinghuan_money=(float)$yuqi+(float)$data['daoqiMoney'];
		return $dingdan_yinghuan_money;
	}
		/**
	* 目前应还   订单应还-部分还款-豁免
	 * $data  订单全部数据
	*/
	public function get_muqian_yinghuan_money($data){
		$get_dingdan_yinghuan_money=$this->get_dingdan_yinghuan_money($data);//订单应还
		$get_jianmian_money=$this->get_jianmian_money($data['OrderSn']);//应还减免
		$get_bufen_money=$this->get_bufen_money($data['OrderSn']);//部分已还
		$muqian_yinghuan_money=$get_dingdan_yinghuan_money-$get_jianmian_money-$get_bufen_money;//目前应还=订单应还-应还减免-部分已还
		return $muqian_yinghuan_money;
	}



}